package com.lcj.odr.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lcj.common.util.Result;
import com.lcj.odr.controller.form.OrderDriverForm;
import com.lcj.odr.controller.form.SearchOrderForm;
import com.lcj.odr.controller.form.UpdateBillFeeForm;
import com.lcj.odr.controller.form.UpdateBillPaymentForm;
import com.lcj.odr.service.OrderBillService;
import com.lcj.odr.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/bill")
@Tag(name = "OrderBillController", description = "订单费用账单Web接口")
public class OrderBillController {
    @Resource
    private OrderBillService orderBillService;

    @Resource
    private UserService userService;

    @PostMapping("/updateBillPayment")
    @Operation(summary = "更新账单实际支付费用")
    public Result updateBillPayment(@RequestBody @Valid UpdateBillPaymentForm form) {
        Map param = BeanUtil.beanToMap(form);
        int rows = orderBillService.updateBillPayment(param);
        return Result.ok().put("rows", rows);
    }
    
    @PostMapping("/updateBillFee")
    @Operation(summary = "更新订单账单费用")
    public Result updateBillFee(@RequestBody @Valid UpdateBillFeeForm form) {
        Map param = BeanUtil.beanToMap(form);
        int rows = orderBillService.updateBillFee(param);
        return Result.ok().put("rows", rows);
    }

    @PostMapping("/searchReviewDriverOrderBill")
    @Operation(summary = "查询司机预览账单")
    public Result searchReviewDriverOrderBill(@RequestBody @Valid OrderDriverForm form) {
        Map param = BeanUtil.beanToMap(form);
        HashMap map = orderBillService.searchReviewDriverOrderBill(param);
        return Result.ok().put("result", map);
    }

    @PostMapping("/searchOpenId")
    @Operation(summary = "查询司机和乘客OpenId")
    public Result searchOpenId(@RequestBody SearchOrderForm form) {
        if(form.getOrderId()==null || form.getCustomerId()==null) {
            return Result.error("缺少customerId或driverId");
        }
        String cstId = userService.searchCustomerOpenId(form.getCustomerId());
        String drId = userService.searchDriverOpenId(form.getDriverId());
        return Result.ok().put("customerOpenId", cstId).put("driverOpenId", drId);
    }
}